'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-01-16 18:07:40
'''
import os
import sys
import json
sys.path.append(os.path.abspath(os.path.join(os.path.dirname('__file__'), os.path.pardir)))

import thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hulk_proxy.HulkProxyServ import Client
from scan_data.ttypes import ScanData

__HOST = "172.21.32.54"
__PORT = 10088

try:
    tsocket = TSocket.TSocket(__HOST, __PORT)
    transport = TTransport.TFramedTransport(tsocket)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Client(protocol)

    transport.open()
    '''
    @note:delete the old models 
    '''
    delete_models = client.get('boss_predict_online_model_names')
    delete_models = delete_models.split(',')
    for name in delete_models:
        # print(name)
        print('model name:', name, 'will be deleted!!')
        client.hdel_all(name)
    


    
    '''
    @note: write model names
    '''
    model1 = 'success_deepfm_d_20200409'
    # model2 = 'success_deepfm_rt_20200507'
    model2 = 'suc_deepfm_id_r_20200528'
    model3 = 'success_df_id_d_20200508'
    # model3 = 'xgboost_20200409'
    model_name_all = model1 + ',' + model2 + ',' + model3
    client.push("boss_predict_online_model_names", model_name_all)
    # tmp = client.get('boss_predict_online_model_names')
    # print(tmp)

    '''
    @note: test model names
    '''
    # a = client.get("boss_predict_online_model_names")
    # print(a)


    '''
    @note: write the first layers
    '''
    layer_f = open('/data1/xuejian/sync/model_zoo/models/galaxy/success_deepfm.net', 'r')
    layer = json.load(layer_f)
    layer_f.close()

    layer = json.dumps(layer)
    # print(type(layer))
    model_config = {'url': '172.21.39.3:8234', 'layer': layer, 'type': 'galaxy'}
    client.hmpush(model1, model_config)


    '''
    @note: write the second layers
    '''
    layer_f = open('/data1/xuejian/sync/model_zoo/models/galaxy/success_deepfm_id.net', 'r')
    layer = json.load(layer_f)
    layer_f.close()

    layer = json.dumps(layer)
    # print(type(layer))
    model_config = {'url': '172.21.39.3:1111', 'layer': layer, 'type': 'galaxy'}
    client.hmpush(model2, model_config)

    '''
    @note: write the second layers
    '''
    layer_f = open('/data1/xuejian/sync/model_zoo/models/galaxy/success_deepfm_id.net', 'r')
    layer = json.load(layer_f)
    layer_f.close()

    layer = json.dumps(layer)
    # print(type(layer))
    model_config = {'url': '172.21.39.3:5555', 'layer': layer, 'type': 'galaxy'}
    client.hmpush(model3, model_config)


    '''
    @note: write the xgboost models ........... model3
    '''
    # feature_f = open('/data1/xuejian/sync/offline_train/xgboost/output/2020-04-08/model/all_cluster/part-00000')
    # feature = feature_f.read()
    # # print(a)
    # feature_f.close()
    # model_conf = {'type': 'xgboost', 'feature':feature}
    # client.hmpush(model3, model_conf)

    # '''
    # @note:write the bin file of xgboost 
    # '''
    # bin_path = '/data1/xuejian/sync/offline_train/xgboost/output/2020-04-08/model/all_cluster/gbdt.bin'
    # command = './cpplib/bazel-bin/write_bin ' + model3 + ' model ' + bin_path
    # os.system(command)


    transport.close()
except thrift.TException as ex:
    print(ex.message)